/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usuario;

import conexion.DB;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Deico
 */
public class login {
    
     public static boolean isUser(String user){


        boolean flag = false;
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("select user from login where user='"+user+"';");
          
          while (rs.next()) {
        
          flag=true;
          }
          rs.close();
          
          return flag;

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
         return flag;
    }
      public static String getPass(String user){


        String pass = "";
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("select pass from login where user='"+user+"';");
          
          while (rs.next()) {
        
          pass = rs.getString("pass");
          }
          rs.close();
          
          return pass;

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
         return null;
    }
      
      public static String getUser(String email){


        String user = "";
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("select user from login where email='"+email+"';");
          
          while (rs.next()) {
        
          user = rs.getString("user");
          }
          rs.close();
          
          return user;

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
         return null;
    }
     
       public static String getTipo(String user){


        String tipo = "";
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("select tipo from login where user='"+user+"';");
          
          while (rs.next()) {
        
          tipo = rs.getString("tipo");
          }
          rs.close();
          
          return tipo;

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
         return null;
    }
     public static String[] getPassTipe(String user){

         
        String aux[] = new String[2];
        try {

          java.sql.Statement stat = DB.DB_CONNECTION.createStatement();
          ResultSet rs = stat.executeQuery("select pass,tipo from login where user='"+user+"';");
          
          while (rs.next()) {
        
          aux[0]=rs.getString("pass");
          aux[1]=rs.getString("tipo");
          }
          rs.close();
          
          return aux;

         } catch (SQLException ex) { 
         JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
         }
         return null;
    }
     
     public static void nuevoUsuario(String user,String pass,String tipo,String email)
 {
                
        try {

            PreparedStatement prep = DB.DB_CONNECTION.prepareStatement(
            "insert into login values (?,?,?,?);");
             
            prep.setString(1,user);
            prep.setString(2,pass);
            prep.setString(3,tipo);
            prep.setString(4,email);

            prep.executeUpdate();
            
            } catch (SQLException ex) {
            System.out.println(ex);
        }
    }
     
     public static void setUpdate(String user,String pass,String email){
         
         try {

            PreparedStatement prep = DB.DB_CONNECTION.prepareStatement(
             "update login set pass=?,email=? where user='"+user +"';");

            prep.setString(1,pass);
            prep.setString(2,email);
            
            prep.executeUpdate();
            
            } catch (SQLException ex) {
            System.out.println(ex);
        }
     }
     
     
     
    
}
